/*
 * @Descripttion:封装axios请求
 * @version:
 * @Author: dlj
 * @Date: 2021-12-20 09:28:43
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-10-08 17:35:09
 */
import axios from 'axios';
import store from '../store';
const root = `${window.location.protocol}//${window.location.host}`;
const config = {
  baseURL: `${root}`,
  // baseURL: "http://localhost:8080/",
  timeout: 30 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
};
const _axios = axios.create(config);

//添加请求拦截器
_axios.interceptors.request.use(
  (config) => {
    // 获取token

    // // 设置参数格式
    if (!config.headers["Content-Type"]) {
      config.headers = {
        "Content-Type": "application/json",
        // application/x-www-form-urlencoded
      };
    }
    const token = store.state.user.token || "";
    if (token != "") {
      config.headers.token = token;
    }
    // 鉴权参数设置
    if (config.method === "get") {
      //get请求下 参数在params中，其他请求在data中
      config.params = config.params || {};
    } else {
      config.data = config.data || {};
      //一些参数处理
    }
    return config
  },
  error => {
    return Promise.reject(error);
  }
)
//添加响应拦截器
_axios.interceptors.response.use(
  response => {
    let res = {}
    res.status = response.data.status
    res.data = response.data.data
    res.message = response.data.message
    return res
  },
  error => {
    return Promise.reject(error);
  }
)

export default _axios